import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher/url_launcher_string.dart';

class LaunchPage extends StatefulWidget {
  const LaunchPage({super.key});

  @override
  State<LaunchPage> createState() => _LaunchPageState();
}

class _LaunchPageState extends State<LaunchPage> {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme:
            ColorScheme.fromSeed(seedColor: ColorUtil.color('#f2f2f2')),
        useMaterial3: true,
      ),
      home: Scaffold(
        appBar: AppBar(
            title: Text('路由'),
            backgroundColor: Theme.of(context).colorScheme.inversePrimary),
        body: RouteNevigator(),
      ),
    );
  }
}

class RouteNevigator extends StatefulWidget {
  @override
  _RouteNevigatorState createState() => _RouteNevigatorState();
}

class _RouteNevigatorState extends State<RouteNevigator> {
  bool byName = false;
  final Uri _url = Uri.parse('https://flutter.dev');

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          TextButton(
              onPressed: () {
                _launchUrl();
              },
              child: Text('打开url')),
          TextButton(
              onPressed: () {
                _openMap();
              },
              child: Text('打开三方应用')),
        ],
      ),
    );
  }

  Future<void> _launchUrl() async {
    if (!await launchUrl(_url)) {
      throw Exception('Could not launch $_url');
    }
  }

  Future<void> _openMap() async {
    final Uri emailLaunchUri = Uri(
      scheme: 'haoyigong',
      path: 'm.haoyigong.com',
    );
    await launchUrl(emailLaunchUri);
  }
}
